/* Copyright (c) 2011 by crossmobile.org
 *
 * CrossMobile is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.
 *
 * CrossMobile is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CrossMobile; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package com.panayotis.xm.frontend.gui;

import com.panayotis.xm.frontend.project.Project;
import com.panayotis.xm.frontend.project.ProjectListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class WelcomeFrame extends javax.swing.JFrame {

    /** Creates new form ProjectManager */
    public WelcomeFrame() {
        initComponents();
        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chooser = new javax.swing.JFileChooser();
        jPanel8 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        CreateB = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        OpenB = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ProjectsL = new javax.swing.JList();
        jPanel4 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        RecentB = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        chooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CrossMobile Manager");
        setResizable(false);

        jPanel8.setBorder(javax.swing.BorderFactory.createEmptyBorder(24, 24, 24, 24));
        jPanel8.setLayout(new java.awt.BorderLayout());

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/logo.jpg"))); // NOI18N
        jLabel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 16));
        jPanel8.add(jLabel1, java.awt.BorderLayout.WEST);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel7.setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 0, 12, 0));
        jPanel7.setLayout(new java.awt.GridLayout(1, 0));

        jPanel5.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 12));
        jPanel5.setLayout(new java.awt.BorderLayout());

        CreateB.setText("Create Project...");
        CreateB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CreateBActionPerformed(evt);
            }
        });
        jPanel5.add(CreateB, java.awt.BorderLayout.CENTER);

        jPanel7.add(jPanel5);

        jPanel6.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 12, 0, 0));
        jPanel6.setLayout(new java.awt.BorderLayout());

        OpenB.setText("Open Project...");
        OpenB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OpenBActionPerformed(evt);
            }
        });
        jPanel6.add(OpenB, java.awt.BorderLayout.CENTER);

        jPanel7.add(jPanel6);

        jPanel1.add(jPanel7, java.awt.BorderLayout.NORTH);

        jPanel3.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.BorderLayout());

        ProjectsL.setModel(new ProjectListModel());
        ProjectsL.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                ProjectsLValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(ProjectsL);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel9.setLayout(new java.awt.BorderLayout());

        RecentB.setText("    Open    ");
        RecentB.setEnabled(false);
        jPanel9.add(RecentB, java.awt.BorderLayout.EAST);

        jLabel2.setText("Recent Projects  ");
        jPanel9.add(jLabel2, java.awt.BorderLayout.WEST);

        jPanel4.add(jPanel9, java.awt.BorderLayout.WEST);

        jPanel2.add(jPanel4, java.awt.BorderLayout.NORTH);

        jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);

        jPanel8.add(jPanel1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel8, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CreateBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CreateBActionPerformed
        chooser.setDialogTitle("Create Project");
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        if (chooser.showDialog(this, "Create") == JFileChooser.APPROVE_OPTION) {
            Project p = new Project(chooser.getSelectedFile().getPath());
            if (p.isWritable()) {
                new ProjectFrame(p).setVisible(true);
                setVisible(false);
                dispose();
            } else
                JOptionPane.showMessageDialog(this, "No CrossMobile project can be created under " + p.getPath(), "Error while creating project", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_CreateBActionPerformed

    private void ProjectsLValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_ProjectsLValueChanged
        RecentB.setEnabled(true);
    }//GEN-LAST:event_ProjectsLValueChanged

    private void OpenBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OpenBActionPerformed
        chooser.setDialogTitle("Open Project");
        chooser.setDialogType(JFileChooser.OPEN_DIALOG);
        if (chooser.showDialog(this, "Open") == JFileChooser.APPROVE_OPTION) {
            Project p = new Project(chooser.getSelectedFile().getPath());
            if (p.exists()) {
                new ProjectFrame(p).setVisible(true);
                setVisible(false);
                dispose();
            } else
                JOptionPane.showMessageDialog(this, "No CrossMobile project exists under " + p.getPath(), "Error while opening project", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_OpenBActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CreateB;
    private javax.swing.JButton OpenB;
    private javax.swing.JList ProjectsL;
    private javax.swing.JButton RecentB;
    private javax.swing.JFileChooser chooser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
